package cn.com.service.impl;

import cn.com.beans.PAccount;
import cn.com.mapper.PAccountMapper;
import cn.com.mapper.RoleMapper;
import cn.com.service.PAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PAccountServiceImpl implements PAccountService {
    @Resource
    private PAccountMapper pAccountMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public PAccount findById(Integer id) {
        return pAccountMapper.findById(id);
    }

    @Transactional
    @Override
    public void insertAccount(PAccount account, Integer[] roleIds) {
        pAccountMapper.insertAccount(account);
        roleMapper.bindRole(account.getId(),roleIds);

    }
    @Transactional
    @Override
    public void updateAcount(PAccount account, Integer[] roleIds) {
        pAccountMapper.updateAcount(account);
        roleMapper.unBindRole(account.getId());
        roleMapper.bindRole(account.getId(),roleIds);
    }

    @Override
    public boolean deleteByIds(Integer... ids) {
        return pAccountMapper.deleteByIds(ids)>0;
    }

    @Override
    public List<PAccount> findAll(PAccount account) {
        return pAccountMapper.findAll(account);
    }

    @Override
    public PAccount findByAccount(String account, String password) {
        return this.pAccountMapper.findByAccount(account,password);
    }







    @Override
    public boolean selectByPhone(String phone) {
        List<PAccount> PAccount = pAccountMapper.selectByPhone(phone);
        System.out.println("PAccount = " + PAccount);
        if(PAccount.size() == 0) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean selectByPhoneAndPwd(PAccount PAccount) {
        List<PAccount> aif = pAccountMapper.selectByPhoneAndPwd(PAccount);
        System.out.println("aif = " + aif);
        if(aif.size() > 0) {
            return true;
        }else{
            return false;
        }
    }



    @Override
    public boolean insert(PAccount PAccount) {
        boolean b = pAccountMapper.insert(PAccount) > 0;
        return b;
    }


    @Override
    public PAccount selectByPrimaryKey(Integer aId) {
        return pAccountMapper.selectByPrimaryKey(aId);
    }



    @Override
    public int updateByPrimaryKey(PAccount PAccount) {
        return pAccountMapper.updateByPrimaryKey(PAccount);
    }
}
